package com.fay.effectivech4;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/2/21 15:09
 * @Version 1.0
 */
public class InstrumentHashSet<E> extends HashSet<E> {
    private int addCount = 0;

    public InstrumentHashSet() {
    }

    public InstrumentHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    @Override
    public boolean add(E e) {
        addCount++;
        return super.add(e);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        addCount += c.size();
        return super.addAll(c);
    }

    public int getAddCount() {
        return addCount;
    }

    public static void main(String[] args) {
        InstrumentHashSet<String> ss = new InstrumentHashSet<>();
        ss.addAll(Arrays.asList("aa", "bb", "cc"));
        System.out.println(ss.addCount);//6
    }
}
